﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
using Ultra_Presenter.Model;

namespace Ultra_Presenter
{
    /// <summary>
    /// Interaction logic for PresenterPayer.xaml
    /// </summary>
    public partial class PresenterPayer : Window
    {
        /// <summary>
        /// Message to command.
        /// </summary>
        public const int WMSYSCOMMAND = 0x0112;

        /// <summary>
        /// Message to close.
        /// </summary>
        public const int SCCLOSE = 0xF060;

        /// <summary>
        /// Send message.
        /// </summary>
        /// <param name="hwnd">Handle da msg.</param>
        /// <param name="msg">Msg PRo</param>
        /// <param name="wparam">ww param.</param>
        /// <param name="lparam">parametro ll.</param>
        /// <returns>Integer with return.</returns>
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hwnd, uint msg, int wparam, int lparam);

        /// <summary>
        /// Dashboard list that´s gonna be executed.
        /// </summary>
        private List<DashboardWrap> dashBoardMovie = new List<DashboardWrap>();

        /// <summary>
        /// Dashboard Enumerator.
        /// </summary>
        private IEnumerator dashBoardMovieEnumerator;

        /// <summary>
        /// Dashboard Wrapper.
        /// </summary>
        private DashboardWrap dbw = new DashboardWrap();

        /// <summary>
        /// Dashboard motion.
        /// </summary>
        private DispatcherTimer dashBoardMotion = new DispatcherTimer();

        /// <summary>
        /// Quadrant motion.
        /// </summary>
        private DispatcherTimer quadrantAMotion = new DispatcherTimer();

        /// <summary>
        /// Quadrant motion.
        /// </summary>
        private DispatcherTimer quadrantBMotion = new DispatcherTimer();

        /// <summary>
        /// Quadrant motion.
        /// </summary>
        private DispatcherTimer quadrantCMotion = new DispatcherTimer();

        /// <summary>
        /// Quadrant motion.
        /// </summary>
        private DispatcherTimer quadrantDMotion = new DispatcherTimer();

        /// <summary>
        /// Project Object.
        /// </summary>
        private Project project;

        ///// <summary>
        ///// Current Dashboard.
        ///// </summary>
        /////private Dashboard currentDashBoard;

        /// <summary>
        /// Index URL.
        /// </summary>
        private int indexQA = 0;

        /// <summary>
        /// Index URL.
        /// </summary>
        private int indexQB = 0;

        /// <summary>
        /// Index URL.
        /// </summary>
        private int indexQC = 0;

        /// <summary>
        /// Index URL.
        /// </summary>
        private int indexQD = 0;

        /// <summary>
        /// URL Object.
        /// </summary>
        private Url[] urlA;

        /// <summary>
        /// URL Object.
        /// </summary>
        private Url[] urlB;

        /// <summary>
        /// URL Object.
        /// </summary>
        private Url[] urlC;

        /// <summary>
        /// URL Object.
        /// </summary>
        private Url[] urlD;

        /// <summary>
        /// Supress javascript erros.
        /// </summary>
        private bool supressJavascriptErros = true;

        /// <summary>
        /// Class constructor.
        /// </summary>
        public PresenterPayer()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Class constructor.
        /// </summary>
        /// <param name="project">Project Object.</param>
        public PresenterPayer(Project project)
        {
            this.InitializeComponent();

            this.dashBoardMotion.Tick += new EventHandler(this.DashBoardMotion_Tick);
            this.dashBoardMotion.IsEnabled = false;

            this.quadrantAMotion.Tick += new EventHandler(this.QuadrantAMotion_Tick);
            this.quadrantAMotion.IsEnabled = false;

            this.quadrantBMotion.Tick += new EventHandler(this.QuadrantBMotion_Tick);
            this.quadrantBMotion.IsEnabled = false;

            this.quadrantCMotion.Tick += new EventHandler(this.QuadrantCMotion_Tick);
            this.quadrantCMotion.IsEnabled = false;

            this.quadrantDMotion.Tick += new EventHandler(this.QuadrantDMotion_Tick);
            this.quadrantDMotion.IsEnabled = false;

            this.project = project;
        }

        /// <summary>
        /// Overrides the on closed event.
        /// </summary>
        /// <param name="e">Object from closed Event.</param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
        }

        /// <summary>
        /// Tick event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void DashBoardMotion_Tick(object sender, EventArgs e)
        {
            this.ShowDashboards();
        }

        /// <summary>
        /// Tick event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void QuadrantDMotion_Tick(object sender, EventArgs e)
        {
            this.quadrantDMotion.Stop();
            this.quadrantDMotion.IsEnabled = false;

            if (this.indexQD < this.urlD.Length)
            {
                switch (this.dbw.Dashboard.Name)
                {
                    case "Ultra_Presenter.UC.Layout_1":
                        this.WD.Navigate(this.urlD[this.indexQD].Name);
                        break;
                    default:
                        break;
                }

                this.quadrantDMotion.Interval = TimeSpan.FromSeconds(this.urlD[this.indexQD].Time);
                this.indexQD++;
            }
            else
            {
                this.indexQD = 0;
                this.quadrantDMotion.Interval = TimeSpan.FromMilliseconds(0);
            }

            this.quadrantDMotion.Start();
            this.quadrantDMotion.IsEnabled = true;
        }

        /// <summary>
        /// Supress Script Errors
        /// </summary>
        /// <param name="wb">Web Browser.</param>
        /// <param name="hide">Hide Errors from javascript.</param>
        public void SuppressScriptErrors(System.Windows.Controls.WebBrowser wb, bool hide)
        {
            FieldInfo fi = typeof(System.Windows.Controls.WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);

            if (fi != null)
            {
                object browser = fi.GetValue(wb);

                if (browser != null)
                {
                    browser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, browser, new object[] { hide });
                }
            }
        }

        /// <summary>
        /// Tick event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void QuadrantCMotion_Tick(object sender, EventArgs e)
        {
            this.quadrantCMotion.Stop();
            this.quadrantCMotion.IsEnabled = false;

            if (this.indexQC < this.urlC.Length)
            {
                switch (this.dbw.Dashboard.Name)
                {
                    case "Ultra_Presenter.UC.Layout_1":
                        this.WC.Navigate(this.urlC[this.indexQC].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_3":
                        this.WC3.Navigate(this.urlC[this.indexQC].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_6":
                        this.WC6.Navigate(this.urlC[this.indexQC].Name);
                        break;
                    default:
                        break;
                }

                this.quadrantCMotion.Interval = TimeSpan.FromSeconds(this.urlC[this.indexQC].Time);
                this.indexQC++;
            }
            else
            {
                this.indexQC = 0;
                this.quadrantCMotion.Interval = TimeSpan.FromMilliseconds(0);
            }

            this.quadrantCMotion.Start();
            this.quadrantCMotion.IsEnabled = true;
        }

        /// <summary>
        /// Tick event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void QuadrantBMotion_Tick(object sender, EventArgs e)
        {
            this.quadrantBMotion.Stop();
            this.quadrantBMotion.IsEnabled = false;

            if (this.indexQB < this.urlB.Length)
            {
                switch (this.dbw.Dashboard.Name)
                {
                    case "Ultra_Presenter.UC.Layout_1":
                        this.WB.Navigate(this.urlB[this.indexQB].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_3":
                        this.WB3.Navigate(this.urlB[this.indexQB].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_4":
                        this.WB4.Navigate(this.urlB[this.indexQB].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_5":
                        this.WB5.Navigate(this.urlB[this.indexQB].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_6":
                        this.WB6.Navigate(this.urlB[this.indexQB].Name);
                        break;
                    default:
                        break;
                }

                this.quadrantBMotion.Interval = TimeSpan.FromSeconds(this.urlB[this.indexQB].Time);
                this.indexQB++;
            }
            else
            {
                this.indexQB = 0;
                this.quadrantBMotion.Interval = TimeSpan.FromMilliseconds(0);
            }

            this.quadrantBMotion.Start();
            this.quadrantBMotion.IsEnabled = true;
        }

        /// <summary>
        /// Mouse event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void QuadrantAMotion_Tick(object sender, EventArgs e)
        {
            this.quadrantAMotion.Stop();
            this.quadrantAMotion.IsEnabled = false;

            if (this.indexQA < this.urlA.Length)
            {
                switch (this.dbw.Dashboard.Name)
                {
                    case "Ultra_Presenter.UC.Layout_1":
                        this.WA.Navigate(this.urlA[this.indexQA].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_2":
                        this.WA2.Navigate(this.urlA[this.indexQA].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_3":
                        this.WA3.Navigate(this.urlA[this.indexQA].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_4":
                        this.WA4.Navigate(this.urlA[this.indexQA].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_5":
                        this.WA5.Navigate(this.urlA[this.indexQA].Name);
                        break;
                    case "Ultra_Presenter.UC.Layout_6":
                        this.WA6.Navigate(this.urlA[this.indexQA].Name);
                        break;
                    default:
                        break;
                }

                this.quadrantAMotion.Interval = TimeSpan.FromSeconds(this.urlA[this.indexQA].Time);
                this.indexQA++;
            }
            else
            {
                this.indexQA = 0;
                this.quadrantAMotion.Interval = TimeSpan.FromMilliseconds(0);
            }

            this.quadrantAMotion.Start();
            this.quadrantAMotion.IsEnabled = true;
        }

        /// <summary>
        /// Methdo for read dashboards in timeline.
        /// </summary>
        private void ReadDashBoardsInTimeline()
        {
            Dashboard d = null;

            foreach (var item in this.project.TimeLine.Dashboards)
            {
                switch (item.Name)
                {
                    case "Ultra_Presenter.UC.Layout_1":
                        d = this.project.Dashboards.Where(x => x.Name.Equals("Ultra_Presenter.UC.Layout_1")).FirstOrDefault();
                        this.dashBoardMovie.Add(new DashboardWrap { Dashboard = d, Timer = this.GetTotalTimeDashBoard(d) });
                        break;

                    case "Ultra_Presenter.UC.Layout_2":
                        d = this.project.Dashboards.Where(x => x.Name.Equals("Ultra_Presenter.UC.Layout_2")).FirstOrDefault();
                        this.dashBoardMovie.Add(new DashboardWrap { Dashboard = d, Timer = this.GetTotalTimeDashBoard(d) });
                        break;

                    case "Ultra_Presenter.UC.Layout_3":
                        d = this.project.Dashboards.Where(x => x.Name.Equals("Ultra_Presenter.UC.Layout_3")).FirstOrDefault();
                        this.dashBoardMovie.Add(new DashboardWrap { Dashboard = d, Timer = this.GetTotalTimeDashBoard(d) });
                        break;

                    case "Ultra_Presenter.UC.Layout_4":
                        d = this.project.Dashboards.Where(x => x.Name.Equals("Ultra_Presenter.UC.Layout_4")).FirstOrDefault();
                        this.dashBoardMovie.Add(new DashboardWrap { Dashboard = d, Timer = this.GetTotalTimeDashBoard(d) });
                        break;

                    case "Ultra_Presenter.UC.Layout_5":
                        d = this.project.Dashboards.Where(x => x.Name.Equals("Ultra_Presenter.UC.Layout_5")).FirstOrDefault();
                        this.dashBoardMovie.Add(new DashboardWrap { Dashboard = d, Timer = this.GetTotalTimeDashBoard(d) });
                        break;

                    case "Ultra_Presenter.UC.Layout_6":
                        d = this.project.Dashboards.Where(x => x.Name.Equals("Ultra_Presenter.UC.Layout_6")).FirstOrDefault();
                        this.dashBoardMovie.Add(new DashboardWrap { Dashboard = d, Timer = this.GetTotalTimeDashBoard(d) });
                        break;

                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// Method to get total time from dashboard.
        /// </summary>
        /// <param name="dash">The dashboard.</param>
        /// <returns>Total Time.</returns>
        private int GetTotalTimeDashBoard(Dashboard dash)
        {
            int sum = 0;
            List<int> lst = new List<int>();

            foreach (var qd in dash.Quadrants)
            {
                foreach (var url in qd.Urls)
                {
                    sum += url.Time;
                }

                lst.Add(sum);
                sum = 0;
            }

            return lst.Max();
        }

        /// <summary>
        /// Shows the dashboards scheduled.
        /// </summary>
        private void ShowDashboards()
        {
            this.dashBoardMotion.Stop();
            this.dashBoardMotion.IsEnabled = false;

            if (this.dashBoardMovieEnumerator.MoveNext())
            {
                this.dbw = (DashboardWrap)this.dashBoardMovieEnumerator.Current;

                this.dashBoardMotion.Interval = TimeSpan.FromSeconds(this.dbw.Timer);

                this.quadrantAMotion.IsEnabled = false;
                this.quadrantAMotion.Stop();

                this.quadrantBMotion.IsEnabled = false;
                this.quadrantBMotion.Stop();

                this.quadrantCMotion.IsEnabled = false;
                this.quadrantCMotion.Stop();

                this.quadrantDMotion.IsEnabled = false;
                this.quadrantDMotion.Stop();

                switch (this.dbw.Dashboard.Name)
                {
                    case "Ultra_Presenter.UC.Layout_1":
                        this.Layout_1.Visibility = System.Windows.Visibility.Visible;
                        this.Layout_2.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_3.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_4.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_5.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_6.Visibility = System.Windows.Visibility.Hidden;

                        this.urlA = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.A).FirstOrDefault().Urls;
                        this.urlB = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.B).FirstOrDefault().Urls;
                        this.urlC = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.C).FirstOrDefault().Urls;
                        this.urlD = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.D).FirstOrDefault().Urls;

                        this.quadrantAMotion.IsEnabled = true;
                        this.quadrantAMotion.Start();
                        this.quadrantAMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantBMotion.IsEnabled = true;
                        this.quadrantBMotion.Start();
                        this.quadrantBMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantCMotion.IsEnabled = true;
                        this.quadrantCMotion.Start();
                        this.quadrantCMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantDMotion.IsEnabled = true;
                        this.quadrantDMotion.Start();
                        this.quadrantDMotion.Interval = TimeSpan.FromMilliseconds(1);

                        break;
                    case "Ultra_Presenter.UC.Layout_2":
                        this.Layout_1.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_2.Visibility = System.Windows.Visibility.Visible;
                        this.Layout_3.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_4.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_5.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_6.Visibility = System.Windows.Visibility.Hidden;

                        this.urlA = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.A).FirstOrDefault().Urls;

                        this.quadrantAMotion.IsEnabled = true;
                        this.quadrantAMotion.Start();
                        this.quadrantAMotion.Interval = TimeSpan.FromMilliseconds(1);

                        break;
                    case "Ultra_Presenter.UC.Layout_3":
                        this.Layout_1.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_2.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_3.Visibility = System.Windows.Visibility.Visible;
                        this.Layout_4.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_5.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_6.Visibility = System.Windows.Visibility.Hidden;

                        this.urlA = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.A).FirstOrDefault().Urls;
                        this.urlB = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.B).FirstOrDefault().Urls;
                        this.urlC = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.C).FirstOrDefault().Urls;

                        this.quadrantAMotion.IsEnabled = true;
                        this.quadrantAMotion.Start();
                        this.quadrantAMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantBMotion.IsEnabled = true;
                        this.quadrantBMotion.Start();
                        this.quadrantBMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantCMotion.IsEnabled = true;
                        this.quadrantCMotion.Start();
                        this.quadrantCMotion.Interval = TimeSpan.FromMilliseconds(1);

                        break;
                    case "Ultra_Presenter.UC.Layout_4":
                        this.Layout_1.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_2.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_3.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_4.Visibility = System.Windows.Visibility.Visible;
                        this.Layout_5.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_6.Visibility = System.Windows.Visibility.Hidden;

                        this.urlA = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.A).FirstOrDefault().Urls;
                        this.urlB = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.B).FirstOrDefault().Urls;

                        this.quadrantAMotion.IsEnabled = true;
                        this.quadrantAMotion.Start();
                        this.quadrantAMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantBMotion.IsEnabled = true;
                        this.quadrantBMotion.Start();
                        this.quadrantBMotion.Interval = TimeSpan.FromMilliseconds(1);

                        break;
                    case "Ultra_Presenter.UC.Layout_5":
                        this.Layout_1.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_2.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_3.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_4.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_5.Visibility = System.Windows.Visibility.Visible;
                        this.Layout_6.Visibility = System.Windows.Visibility.Hidden;

                        this.urlA = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.A).FirstOrDefault().Urls;
                        this.urlB = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.B).FirstOrDefault().Urls;

                        this.quadrantAMotion.IsEnabled = true;
                        this.quadrantAMotion.Start();
                        this.quadrantAMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantBMotion.IsEnabled = true;
                        this.quadrantBMotion.Start();
                        this.quadrantBMotion.Interval = TimeSpan.FromMilliseconds(1);

                        break;
                    case "Ultra_Presenter.UC.Layout_6":
                        this.Layout_1.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_2.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_3.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_4.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_5.Visibility = System.Windows.Visibility.Hidden;
                        this.Layout_6.Visibility = System.Windows.Visibility.Visible;

                        this.urlA = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.A).FirstOrDefault().Urls;
                        this.urlB = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.B).FirstOrDefault().Urls;
                        this.urlC = this.dbw.Dashboard.Quadrants.Where(x => x.Name == QuadrantName.C).FirstOrDefault().Urls;

                        this.quadrantAMotion.IsEnabled = true;
                        this.quadrantAMotion.Start();
                        this.quadrantAMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantBMotion.IsEnabled = true;
                        this.quadrantBMotion.Start();
                        this.quadrantBMotion.Interval = TimeSpan.FromMilliseconds(1);

                        this.quadrantCMotion.IsEnabled = true;
                        this.quadrantCMotion.Start();
                        this.quadrantCMotion.Interval = TimeSpan.FromMilliseconds(1);

                        break;
                    default:
                        break;
                }
            }
            else
            {
                this.dashBoardMovieEnumerator.Reset();
            }

            this.InvalidateVisual();
            this.dashBoardMotion.Start();
            this.dashBoardMotion.IsEnabled = true;
        }

        /// <summary>
        /// WIndow Loaded event
        /// </summary>
        /// <param name="sender">Object sender.</param>
        /// <param name="e">Parameter sent.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.ReadDashBoardsInTimeline();
            this.dashBoardMovieEnumerator = this.dashBoardMovie.GetEnumerator();

            this.dashBoardMotion.Interval = TimeSpan.FromMilliseconds(1);
            this.dashBoardMotion.IsEnabled = true;
            this.dashBoardMotion.Start();
        }

        /// <summary>
        /// Navigated event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void WXX_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            this.SuppressScriptErrors((System.Windows.Controls.WebBrowser)sender, this.supressJavascriptErros);
        }

        /// <summary>
        /// Navigated event.
        /// </summary>
        /// <param name="sender">Sender object</param>
        /// <param name="e">Event sent.</param>
        private void Window_Closed(object sender, EventArgs e)
        {
            this.dashBoardMotion.Stop();
            this.dashBoardMotion.IsEnabled = false;
            this.dashBoardMotion = null;

            this.quadrantAMotion.Stop();
            this.quadrantAMotion.IsEnabled = false;
            this.quadrantAMotion = null;

            this.quadrantBMotion.Stop();
            this.quadrantBMotion.IsEnabled = false;
            this.quadrantBMotion = null;

            this.quadrantCMotion.Stop();
            this.quadrantCMotion.IsEnabled = false;
            this.quadrantCMotion = null;

            this.quadrantDMotion.Stop();
            this.quadrantDMotion.IsEnabled = false;
            this.quadrantDMotion = null;
        }
    }
}
